package com.smy.jkds.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.EncryptUtils;
import com.smy.jkds.po.*;
import com.smy.jkds.service.JkdsUserService;
import com.smy.jkds.vo.JkdsUserVO;
import com.smy.jkds.vo.UpdatePasswordVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;

import javax.validation.constraints.NotNull;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author zoum
 * @since 2021-10-31
 */
@Controller
@RequestMapping("/jkds/user")
public class JkdsUserController {

    @Autowired
    JkdsUserService jkdsUserService;


    @PostMapping("createUser")
    public Result createUser(@RequestBody JkdsUser jkdsUser){
        if (StringUtils.isBlank(jkdsUser.getMobile())){
            throw new RuntimeException("用户手机号不能为空");
        }
        if (jkdsUser.getAccountNo() == null){
            jkdsUser.setAccountNo(jkdsUser.getMobile());
        }

        if (StringUtils.isBlank(jkdsUser.getPassword())){
            throw new RuntimeException("用户密码不能为空");
        }

        jkdsUser.setSalt(LocalDate.now().getDayOfYear()+"");
        String password = EncryptUtils.md5Base64(jkdsUser.getPassword() + jkdsUser.getSalt());
        jkdsUser.setPassword(password);
        boolean r = jkdsUserService.save(jkdsUser);
        return r ? Result.success("用户创建成功"): Result.error("用户创建失败");
    }

    @PostMapping("updateUser")
    public Result updateUser(@RequestBody @NotNull JkdsUser jkdsUser){
        if (jkdsUser.getId() == null){
            throw new RuntimeException("用户ID不能为空");
        }

        boolean r = jkdsUserService.updateById(jkdsUser);
        return r ? Result.success("用户更新成功"): Result.error("用户更新失败");
    }

    @GetMapping("queryUserList")
    public Result queryUserList(String id, String vip, String accountNo, String mobile){
        JkdsUser user = new JkdsUser();
        user.setId(id);
        user.setId(vip);
        user.setId(accountNo);
        user.setId(mobile);
        List<JkdsUser> jkdsUserList = jkdsUserService.list(user.getQueryWrapper());
        return Result.success("查询成功", jkdsUserList.stream().map(u -> {
            JkdsUserVO vo = new JkdsUserVO();
            BeanUtil.copyProperties(u, vo);
            return vo;
        }).collect(Collectors.toList()));
    }

    @PostMapping("updatePassword")
    public Result updatePassword(@RequestBody UpdatePasswordVO param){
        if (StringUtils.isAllBlank(param.getAccountNo(),
                param.getMobile(), param.getId())){
            throw new RuntimeException("参数缺失，请确认！");
        }

        if (StringUtils.isAnyBlank(param.getPassword(), param.getPasswordAgain())){
            throw new RuntimeException("新密码和确认密码不能为空");
        }

        if (!StringUtils.equals(param.getPassword(), param.getPasswordAgain())){
            throw new RuntimeException("新密码和确认密码不一致");
        }

        if (StringUtils.isBlank(param.getOldPassword())){
            throw new RuntimeException("原密码不能为空");
        }

        JkdsUser user = new JkdsUser();
        user.setId(param.getId());
        user.setMobile(param.getMobile());
        user.setAccountNo(param.getAccountNo());
        JkdsUser oldUser = jkdsUserService.getOne(user.getQueryWrapper());
        if (oldUser == null){
            throw new RuntimeException("用户不存在。");
        }

        String oldPassword = EncryptUtils.md5Base64(param.getOldPassword() + oldUser.getSalt());
        if (!StringUtils.equals(oldUser.getPassword(), oldPassword)){
            throw new RuntimeException("新密码和确认密码不一致");
        }

        String newPassword =  EncryptUtils.md5Base64(param.getPassword() + oldUser.getSalt());
        user.setPassword(newPassword);
        user.setId(param.getId());
        user.setMobile(param.getMobile());
        user.setAccountNo(param.getAccountNo());
        boolean r = jkdsUserService.update(user.getQueryWrapper());
        return r ? Result.success("用户密码修改成功"): Result.error("用户密码修改失败");
    }

    @PostMapping("login")
    public Result login(@RequestBody JkdsUser jkdsUser){
        if (jkdsUser.getId() == null){
            throw new RuntimeException("用户ID不能为空");
        }

        boolean r = jkdsUserService.updateById(jkdsUser);
        return r ? Result.success("用户更新成功"): Result.error("用户更新失败");
    }


    @GetMapping("logout")
    public Result logout(@CurrentUser JkdsUser user){
        if (user == null || StringUtils.isBlank(user.getId())){
            throw new RuntimeException("用户ID不能为空");
        }
        boolean r = jkdsUserService.updateById(user);
        return r ? Result.success("用户更新成功"): Result.error("用户更新失败");
    }


}

